package com.fowo.api.documentCirculation.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fowo.api.common.util.ContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;

/**
 * 用于处理调用其它服务
 */
@Component
public class DataTaskServiceInvoker {
    public void invoke(String serverName, String methodName, Object arg) throws Exception {
        Object service = ContextHolder.getApplicationContext().getBean(serverName);
        Method method = ReflectionUtils.findMethod(service.getClass(), methodName, JSONObject.class);
        if (method == null) {
            throw new Exception(String.format("未在服务类 %s 中找到指定调用方法名 %s", service.getClass().getName(), methodName));
        }
        Object invokeArg = arg instanceof JSONObject ? arg : JSONObject.toJSON(arg);
        method.invoke(service, invokeArg);
    }
}
